package com.kanshu.ksgb.fastread.doudou.common.business.ad.dialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Space;
import android.widget.TextView;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.AdStyleBean;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.AdUtils;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.SplashAdListener;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.retrofit.ADConfigBean;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Timer;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by xubo on 2018/11/22
 */

public class AdHotSplashDialog extends Dialog implements SplashAdListener {

    private static final String SKIP_TEXT = "点击跳过 %d";

    @BindView(R.id.ad_container_layout)
    ViewGroup mAdContainer;
    @BindView(R.id.skipView)
    TextView mSkipView;
    @BindView(R.id.skipLayout)
    Space mSkipLayout;
    @BindView(R.id.app_logo)
    View mLogoView;
    private Timer mTimer;

    //广告是否load完成
    private boolean isAdLoadFinish;

    public AdHotSplashDialog(@NonNull Context context) {
        super(context, R.style.full_dialog_style);
        init(context);
    }

    public AdHotSplashDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }

    protected AdHotSplashDialog(@NonNull Context context, boolean cancelable, @android.support.annotation.Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    @SuppressLint("InflateParams")
    private void init(Context context){
        setContentView(R.layout.layout_splash_embed_page);

        ButterKnife.bind(this);
        DisplayUtils.visible(mAdContainer);
        DisplayUtils.invisible(mSkipView);
        setOnShowListener(dialog -> AdUtils.fetchAdUtil((Activity) context, mAdContainer, mSkipView, ADConfigBean.POSITION_SPACE_TIME_SPLASH, AdStyleBean.SPLASH_STYLE, 0, AdHotSplashDialog.this));
        //设置dialog的宽高为屏幕的宽高
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION);
        setCanceledOnTouchOutside(false);
        setCancelable(false);
    }

    @Override
    public void dismiss() {
        if (mTimer != null) {
            mTimer.cancel();
        }
        super.dismiss();
    }

    @Override
    @SuppressLint("DefaultLocale")
    public void onADTick(long millisUntilFinished) {
        mSkipView.setText(String.format(SKIP_TEXT, Math.round(millisUntilFinished / 1000f)));
    }

    @Override
    public void onFastADClosed(@NotNull Timer timer) {
        this.mTimer = timer;
    }

    @Override
    public void isShowSkipView(boolean isShow) {
        if (isShow) {
            mSkipView.setBackgroundResource(R.drawable.ad_background_circle);
            DisplayUtils.visible(mSkipView, mSkipLayout);

            mSkipLayout.setOnClickListener(v -> {
                if (mAdContainer.getVisibility() == View.VISIBLE) {
                    dismiss();
                }
            });
        } else {
            DisplayUtils.invisible(mSkipView, mSkipLayout);
        }
    }

    @Override
    public void onAdLoadSucceeded(@Nullable View view) {
        isAdLoadFinish = true;
        DisplayUtils.visible(mLogoView);
    }

    @Override
    public void onAdLoadFailed() {
        dismiss();
    }

    @Override
    public void onAdClicked() {
    }

    @Override
    public void onADClosed() {
        dismiss();
    }

    @Override
    public void onBackAd(@NotNull Object ad) {
    }

    @Override
    public void onJump(boolean isJump) {
    }

    @Override
    public void onSplashHolder(boolean isShow) {
    }

    @OnClick({R.id.rel_ad})
    public void clicked(View view){
        if(view.getId() == R.id.rel_ad){
            // 点击dialog 延时1秒处理
            AndroidSchedulers.mainThread().scheduleDirect(() -> {
                if(!isAdLoadFinish){
                    dismiss();
                }
            }, 1000, TimeUnit.MILLISECONDS);
        }
    }

}
